package com.gitee.sop.layui.adminserver.module.background.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysMenu;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysMenuRole;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysMenuRoleRelation;
import com.gitee.sop.layui.adminserver.module.background.admin.mapper.SysMenuRoleRelationMapper;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysDataModifyLockService;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysMenuRoleRelationService;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysMenuService;
import com.gitee.sop.layui.adminserver.module.background.admin.util.SysDataModifyLockUtil;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单mapping 服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-13
 */
@Service
public class SysMenuRoleRelationServiceImpl extends ServiceImpl<SysMenuRoleRelationMapper, SysMenuRoleRelation> implements SysMenuRoleRelationService {
    @Lazy
    @Resource
    private SysMenuService sysMenuService;

    @Lazy
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;

    @Override
    public List<String> getMenuUuidByRoleUuid(String roleUuid) {
        return this.baseMapper.listMenuUuidByRoleUuid(roleUuid);
    }

    @Override
    public int countInMenuRoleUuids(List<String> menuRoleUuidList) {
        return baseMapper.countInMenuRoleUuids(menuRoleUuidList);
    }

    @Override
    public int countInMenuUuids(List<String> menuUuidList) {
        return baseMapper.countInMenuUuids(menuUuidList);
    }

    @Override
    public List<SysMenuRoleRelation> getListInRoleUuid(List<String> menuRoleUuidList) {
        return this.baseMapper.selectInRoleUuid(menuRoleUuidList);
    }

    @Override
    public void setAuthority(String roleUuid, List<Integer> menuIdList, String appId) {
        sysDataModifyLockService.checkLocked(SysDataModifyLockUtil.generateKey(SysMenuRole.class, roleUuid));

        String realAppId = ObjectUtil.isEmpty(appId) ? ServiceContext.me().getCurrentApplication().getAppId() : appId;

        //删除老的权限
        baseMapper.deleteByRoleUuid(roleUuid);
        if (!menuIdList.isEmpty()) {
            //TODO 检索添加菜单是否为当前用户可见菜单，目的：防止被恶意攻击刷取用户权限
            List<SysMenu> menuList = sysMenuService.getListByAppIdAndIds(realAppId, menuIdList);

            List<SysMenuRoleRelation> list = menuList.stream().collect(ArrayList::new, (res, ele) -> res.add(new SysMenuRoleRelation(roleUuid, ele.getUuid())), (l, r) -> {
            });
            this.saveBatch(list);
        }
    }
}
